--[[
连接上zk后，发送了 ConnectRequest,并获取了响应
--]]

local sock = require('socket')
local jute = require('jute')
local myUtils = require('myutils')

local host = 'localhost'
local port = 2181
local status = {
  zxid = 0,               -- Uint8Array[8]
  sessionTimeout = 30000,
  sessionId = 0 ,         -- Uint8Array[8]
  sessionPassword = jute.makeBuffer(16)    -- Uint8Array[16]
}

local request = jute.pack(
  "ConnectRequest",
  jute.PROTOCOL_VERSION,
  status.zxid,
  status.sessionTimeout,
  status.sessionId,
  status.sessionPassword
)

-- myUtils.writeFile(request)

local conn = assert(sock.connect(host, port))
conn:send(request)

local b1, status, partial = conn:receive(4)
-- myUtils.writeFile(b1)

-- if b1 == nil then
--   print("receive b1 = nil")
--   return
-- end

local length = jute.unpackInt(b1)
print('length='..length)

local b2, status, partial = conn:receive(length)
myUtils.writeFile(b2)


conn:close()
